﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Extensions;
using DotSpatial.Controls;
using DotSpatial.Controls.Header;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using HydroDesktop.Interfaces;
using HydroDesktop.Common.Tools;
using HydroDesktop.Interfaces.PluginContracts;

namespace DroughtAnalysis
{
    /// <summary>
    /// The drought analysis plugin main entry point
    /// </summary>
    public class DroughtAnalysisPlugin:Extension
    {
        const string droughtAnalysisPluginKey = "kDroughtAnalysis";
        const string droughtAnalysisPluginName = "Drought Analysis";

        //the seriesView component
        [Import("SeriesControl", typeof(ISeriesSelector))]
        internal ISeriesSelector SeriesControl { get; set; }

        private DroughtSettings UserSettings = new DroughtSettings();
        /// <summary>
        /// Activates this extension
        /// </summary>
        public override void Activate()
        {
            var droughtMenu = new MenuContainerItem(HeaderControl.HomeRootItemKey, droughtAnalysisPluginKey, "Drought Analysis");
            droughtMenu.GroupCaption = droughtAnalysisPluginName;
            droughtMenu.LargeImage = Properties.Resources.drought_fire_32;
            
            App.HeaderControl.Add(droughtMenu);

            var btnMeteoDrought = new SimpleActionItem(HeaderControl.HomeRootItemKey, droughtAnalysisPluginKey, "Meteorological Drought", btnMeteoDrought_Click);
            btnMeteoDrought.GroupCaption = droughtMenu.GroupCaption;
            App.HeaderControl.Add(btnMeteoDrought);

            var btnHydroDrought = new SimpleActionItem(HeaderControl.HomeRootItemKey, droughtAnalysisPluginKey, "Hydrological Drought", btnHydroDrought_Click);
            btnHydroDrought.GroupCaption = droughtMenu.GroupCaption;
            App.HeaderControl.Add(btnHydroDrought);
            
            base.Activate();
        }
        /// <summary>
        /// Deactivates this extension
        /// </summary>
        public override void Deactivate()
        {
            App.HeaderControl.RemoveAll();
            base.Deactivate();
        }

        void btnMeteoDrought_Click(object sender, EventArgs e)
        {
            if (formIsRunning) return;
            
            StationFinder sf = new StationFinder(App.Map);
            var res = sf.FindSuitableStations().OrderBy(s => s.Name);

            if (!res.Any())
            {
                MessageBox.Show("No Sites with Temperature and precipitation found. Please download data first.");
                return;
            }
            try
            {
                var startDate = sf.FindMinimumSuitableDate("MIN","BeginDateTime");
                var endDate = sf.FindMinimumSuitableDate("MAX","EndDateTime");
                
                UserSettings.SuitableSites = res.ToList();
                UserSettings.StartDate = startDate;
                UserSettings.EndDate = endDate;
                SelectStationForm frm = new SelectStationForm(UserSettings, SeriesControl, App.Map as Map, App.GetExtension<ISearchPlugin>());
                frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
                frm.Show();
                formIsRunning = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            formIsRunning = false;
        }

        void btnHydroDrought_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This operation is not yet implemented");
        }


        bool formIsRunning = false;
    }
    
}
